package blms.system;

import java.io.File;

import blms.system.exception.UnknownUserAttribute;

public class User {

	private String email;
	private String firstName;
	private String lastName;
	private String homePhone;
	private String workPhone;
	private String cellPhone;
	private File picture;

	/**
	 * Creates a new User using the given parameters.
	 * @param firstName
	 * @param lastName
	 * @param homePhone
	 * @param workPhone
	 * @param cellPhone
	 * @param email
	 * @param picture
	 */
	public User(String firstName, String lastName, String homePhone,
			String workPhone, String cellPhone, String email, File picture) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.homePhone = homePhone;
		this.workPhone = workPhone;
		this.cellPhone = cellPhone;
		this.email = email;
		this.picture = picture;
		
	}

	/**
	 * Returns the email of this <code>User</code>. The email of a user is
	 * his identifier for the system. Calling this method has the same effect that calling 
	 * getID() method.
	 * @return The email of this <code>User</code>.
	 */
	public String getEmail() {
		return this.email;
	}
	
	/**
	 * Returns the id of the user. Calling this method has the same effect that calling 
	 * getID() method.
	 * @return The id of the user.
	 */
	public String getID() {
		return this.getEmail();
	}

	/**
	 * Returns the value of the required attribute.
	 * @param attribute The required attribute.
	 * @return The value of the required attribute.
	 */
	public String getAttribute(UserAttributes attribute) throws UnknownUserAttribute {
		if (attribute.equals(UserAttributes.FIRST_NAME)) return this.firstName;
		if (attribute.equals(UserAttributes.LAST_NAME)) return this.lastName;
		if (attribute.equals(UserAttributes.HOME_PHONE)) return this.homePhone;
		if (attribute.equals(UserAttributes.WORK_PHONE)) return this.workPhone;
		if (attribute.equals(UserAttributes.CELL_PHONE)) return this.cellPhone;
		if (attribute.equals(UserAttributes.EMAIL)) return this.email;
		if (attribute.equals(UserAttributes.PICTURE)) return this.picture.getName();
		throw new UnknownUserAttribute();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((cellPhone == null) ? 0 : cellPhone.hashCode());
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result
				+ ((homePhone == null) ? 0 : homePhone.hashCode());
		result = prime * result
				+ ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result + ((picture == null) ? 0 : picture.hashCode());
		result = prime * result
				+ ((workPhone == null) ? 0 : workPhone.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final User other = (User) obj;
		if (cellPhone == null) {
			if (other.cellPhone != null)
				return false;
		} else if (!cellPhone.equals(other.cellPhone))
			return false;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (firstName == null) {
			if (other.firstName != null)
				return false;
		} else if (!firstName.equals(other.firstName))
			return false;
		if (homePhone == null) {
			if (other.homePhone != null)
				return false;
		} else if (!homePhone.equals(other.homePhone))
			return false;
		if (lastName == null) {
			if (other.lastName != null)
				return false;
		} else if (!lastName.equals(other.lastName))
			return false;
		if (picture == null) {
			if (other.picture != null)
				return false;
		} else if (!picture.equals(other.picture))
			return false;
		if (workPhone == null) {
			if (other.workPhone != null)
				return false;
		} else if (!workPhone.equals(other.workPhone))
			return false;
		return true;
	}

	void changeAttribute(UserAttributes attribute, String value) {
		if (attribute.equals(UserAttributes.FIRST_NAME)) this.firstName = value;
		if (attribute.equals(UserAttributes.LAST_NAME)) this.lastName = value;
		if (attribute.equals(UserAttributes.HOME_PHONE)) this.homePhone = value;
		if (attribute.equals(UserAttributes.WORK_PHONE)) this.workPhone = value;
		if (attribute.equals(UserAttributes.CELL_PHONE)) this.cellPhone = value;
		if (attribute.equals(UserAttributes.EMAIL)) this.email = value;
		if (attribute.equals(UserAttributes.PICTURE)) this.picture = new File(value);
	}
}
